#ifndef Menu_First
#define Menu_First
#ifdef __GNUG__
#pragma interface
#endif

#include "Manager.h"
#include "CompVObject.h"
#include "PopupItem.h"
#include "Separator.h"

class Clipper;

//---- MenuButtonItem ----------------------------------------------------------

const int cItemStateOneOf = 1,
		cItemStateManyOf= 2;
		
class MenuButtonItem : public StateButton {
public:
	MetaDef(MenuButtonItem);
	MenuButtonItem(int id, const char *l= 0, const char *s= 0);
	MenuButtonItem(int id, VObject *l, const char *s= 0);
	Metric GetMinSize();
	void SetOrigin(Point);
	void SetExtent(Point e);
	void DrawInner(Rectangle r, bool highlight);
	const char *AsString();
	void SetShortCut(const char *sc);
	void SetLabel(const char *s);
	void SetString(const char *s);
	void InputKbd(Token &t);
	int Compare(Object*);
};

//---- Menu --------------------------------------------------------------------

enum MenuOptions {
	eMenuTitle      = BIT(eEvtLast+1),       // show title
	eMenuNoScroll   = BIT(eEvtLast+2),
	eMenuDefault    = eMenuTitle,
	eMenuLast       = eEvtLast + 2
};

class Menu : public Manager {
public:
	MetaDef(Menu);
	Menu(const char *t= 0, bool dimall= TRUE, bool resetAll= TRUE,
				int r= 0, int c= 1, SeqCollection *col= 0);
	Menu(int id, const char *t= 0, bool dimall= TRUE, bool resetAll= TRUE,
				int r= 0, int c= 1, SeqCollection *col= 0);
	~Menu();

	//---- initialize
	Window *DoMakeWindows();
	VObject *FindItem(int id);
	SeqCollection *GetCollection();
	void SetCollection(SeqCollection *col, bool freeold= TRUE);
	VObject *FindInitialItem(int id);
	void Control(int id, int part, void *val);
	VObject *FindAnItem(Point p);
	int CheckKey(Token &t);
	void SetNextHandler(EvtHandler *eh);
	EvtHandler *GetNextHandler();

	//---- show/hide
	virtual int Pulldown(Point, VObject *v= 0);
	int Popup(Point, VObject*, int id, bool showlast, int menupos,
								Rectangle exitr);
	Point GetInitialPos(Point e);

	//---- submenues
	void AppendMenu(Menu*, int id= 999);

	//---- inserting items
	void Insert(VObject*);
	void InsertBefore(int id, VObject*);
	void InsertAfter(int id, VObject*);
	void InsertItemBefore(int atId, const char*, int myId);
	void InsertItemAfter(int atId, const char*, int myId);
	void InsertItemsBefore(int atId, ...);
	void InsertItemsAfter(int atId, ...);
	void InsertItem(const char*, int);
	virtual VObject *MakeMenuItem(const char *s, int id);

	//---- adding items
	void Append(VObject*);
	void AppendItem(const char*, int);
	void AppendItems(const char*, ...);

	//---- replacing items
	void ReplaceItem(int id, const char *label);
	void ToggleItem(int id, bool toggle, const char *toggleTrue,
							const char *toggleFalse);
	void ToggleItem(int id, bool toggle, const char *fmt,
				const char *toggleTrue, const char *toggleFalse);

	//---- removing items
	VObject *RemoveItem(int id);
	void RemoveAllItems(bool free= TRUE);

	//---- enable/disable items
	void EnableItem(int, bool b= TRUE);
	void EnableItems(int id, ...);

	void EnableAll(bool b= TRUE);
	void DisableAll()
		{ EnableAll(FALSE); }
	void ResetAll();
	void DisableItem(int i)
		{ EnableItem(i, FALSE); }

	void CheckItem(int id, int state= 1, int type= cItemStateOneOf);

	//---- set selected item
	int GetSelectedId()
		{ return selection; }
	void SetSelectedItem(int);

	//---- find items
	Menu *FindMenuItem(int);

	//---- input/output
	OStream& PrintOn(OStream&);
	IStream& ReadFrom(IStream&);

protected:
	void InsertVItems(int atId, bool before, const char *first, va_list ap);
	void Init(int id, const char *t, bool da, bool ra, int r, int c,
							SeqCollection *col);

protected:
	VObject *view;
	int selection;
	Clipper *clipper;
	bool resetAll;
	bool dimAll;
	EvtHandler *nextHandler;
};

#endif
